﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using shfa.snomtastic.data.security;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;


namespace shfa.snomtastic.data.business
{
    public class PhoneCommand
    {
        public enum CommandType { Reregister, Reboot, Reset }

        public PhoneInfo Phone { get; set; }
        public string CommandText { get; set; }
        public Guid PhoneId { get; set; }
        public Guid UserId { get; set; }

        public PhoneCommand(PhoneInfo pi, CommandType cmd)
        {
           
            this.Phone = pi;
            this.CommandText = getCommandText(cmd);

          
            
        }


        public PhoneCommand(string ipAddress, CommandType cmd)
        {

            PhoneController pc = new PhoneController();
            List<PhoneInfo> piList = pc.SelectPhones();
            foreach (PhoneInfo pi in piList)
            {
                if (pi.IpAddress == ipAddress)
                    this.Phone = pi;
            }
           
            this.CommandText = getCommandText(cmd);

        }



        public void SendCommand()
        {
            AppConfig ac = new AppConfig();
            RijndaelEnhanced re = new RijndaelEnhanced();
            string httpUser = ac.Items["http_user"];
            string httpPass = re.Decrypt(ac.Items["http_pass"]);

            if (Phone!=null)
            {

                ServicePointManager.ServerCertificateValidationCallback +=
                  delegate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
                  { return true; };

                WebRequest req = HttpWebRequest.Create("https://" + Phone.IpAddress + CommandText);
                NetworkCredential nc = new NetworkCredential(httpUser, httpPass);
                
                req.PreAuthenticate = true;
                req.Credentials = nc;
                
                try { WebResponse resp = req.GetResponse(); }
                catch
                {
                    try { WebResponse resp = req.GetResponse(); }
                    catch
                    {
                        try { WebResponse resp = req.GetResponse(); }
                        catch (Exception ex)
                        {
                            EventLog el = new EventLog();
                            el.LogEvent(Phone.PhoneId, Phone.UserId, "", Phone.IpAddress, "", "Phone Command", ex.Message, "Error");
                        }
                    }
                }
                 
            }

        }

        private string getCommandText(CommandType ct)
        {
            switch (ct)
            {
                case CommandType.Reregister:
                    return "/line_login.htm?REREGISTER:1=Re-Register";
                case CommandType.Reboot:
                    return "/advanced.htm?reboot=reboot";
                case CommandType.Reset:
                    return "/advanced.htm?reset=reset";
                default:
                    return "";
            }
        }


    }
}
